package com.blankm.codec_tools;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankm.codec_tools.databinding.ActivityMainBinding;
import com.google.android.exoplayer2.mediacodec.MediaCodecInfo;
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil;
import com.google.android.exoplayer2.util.MimeTypes;
import cn.blankm.videokernel.factory.PlayerFactory;
import cn.blankm.videokernel.impl.exo.ExoMediaPlayer;
import cn.blankm.videokernel.impl.exo.ExoPlayerFactory;
import cn.blankm.videokernel.impl.ijk.IjkPlayerFactory;
import cn.blankm.videokernel.impl.ijk.IjkVideoPlayer;
import cn.blankm.videokernel.impl.media.AndroidMediaPlayer;
import cn.blankm.videokernel.impl.media.MediaPlayerFactory;
import cn.blankm.video.config.ConstantKeys;
import cn.blankm.video.config.VideoPlayerConfig;
import cn.blankm.video.player.VideoViewManager;

import java.lang.reflect.Field;
import java.util.List;


/**
 * Copyright (C), 1999-2022
 * Author qfmeng@iflytek.com
 * Date 2022/6/15 17:06
 * <p>
 * Description:
 */
public class MainActivity extends AppCompatActivity {


    ActivityMainBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        String log = "设备相关信息\n";
        //系统相关信息
        log = log + "\n系统版本号==> " + DeviceUtils.getSDKVersionName()
                + "\n系统版本码==> " + DeviceUtils.getSDKVersionCode()
                + "\n设备厂商==> " + DeviceUtils.getManufacturer()
                + "\n设备型号==> " + DeviceUtils.getModel()
                + "\n设备stbid==> " + MySystemProperties.getStbid();


        //解码器相关信息
        log = log + "\n\n\n=================================\n\n\n" +
                "\n检测设置是否支持H264解码\n";
        try {
            List<MediaCodecInfo> decoderInfos = MediaCodecUtil.getDecoderInfos(MimeTypes.VIDEO_H264, false, false);
            log = log + "\n可用解码器数量==> " + decoderInfos.size();
            for (MediaCodecInfo info1 : decoderInfos) {
                log = log + "\n-----------------------------------"
                        + "\n解码器的名称==> " + info1.name
                        + "\n是否硬件加速==> " + (info1.hardwareAccelerated ? "是" : "否")
                        + "\n是否仅为软编解码器==> " + (info1.softwareOnly ? "是" : "否")
                        + "\n由编解码器处理的MIME类型==> " + info1.codecMimeType;
            }
            binding.textContent.setText(log);
        } catch (MediaCodecUtil.DecoderQueryException e) {
            e.printStackTrace();
            log = log + "\n此设备不支持H264硬解码";
            binding.textContent.setText(log + "[" + e.getMessage() + "]");
        }


        binding.btnExo.setOnClickListener(v -> {
            setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_EXO);
            ActivityUtils.startActivity(PlayerActivity.class);
        });

        binding.btnIjk.setOnClickListener(v -> {
            setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_IJK);
            ActivityUtils.startActivity(PlayerActivity.class);
        });

        binding.btnDouble.setOnClickListener(v -> {
            setChangeVideoType(ConstantKeys.VideoPlayerType.TYPE_EXO);
            ActivityUtils.startActivity(DoublePlayerActivity.class);
        });
    }


    @SuppressLint("SetTextI18n")
    private void setChangeVideoType(@ConstantKeys.PlayerType int type) {
        //切换播放核心，不推荐这么做，我这么写只是为了方便测试
        VideoPlayerConfig config = VideoViewManager.getConfig();
        try {
            Field mPlayerFactoryField = config.getClass().getDeclaredField("mPlayerFactory");
            mPlayerFactoryField.setAccessible(true);
            PlayerFactory playerFactory = null;
            switch (type) {
                case ConstantKeys.VideoPlayerType.TYPE_IJK:
                    playerFactory = IjkPlayerFactory.create();
                    IjkVideoPlayer ijkVideoPlayer = (IjkVideoPlayer) playerFactory.createPlayer(this);
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_EXO:
                    playerFactory = ExoPlayerFactory.create();
                    ExoMediaPlayer exoMediaPlayer = (ExoMediaPlayer) playerFactory.createPlayer(this);
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_NATIVE:
                    playerFactory = MediaPlayerFactory.create();
                    AndroidMediaPlayer androidMediaPlayer = (AndroidMediaPlayer) playerFactory.createPlayer(this);
                    break;
                case ConstantKeys.VideoPlayerType.TYPE_RTC:
                    break;
            }
            mPlayerFactoryField.set(config, playerFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
